package cn.zswltech.gruul.web.api.controller;

import cn.zswltech.gruul.api.CustomerTreeApi;
import cn.zswltech.gruul.biz.service.CustomTreeService;
import cn.zswltech.gruul.common.dto.*;
import cn.zswltech.gruul.common.exception.BaseException;
import cn.zswltech.gruul.common.exception.MenuErrorEnum;
import cn.zswltech.gruul.common.exception.MenuException;
import cn.zswltech.gruul.common.result.Response;
import cn.zswltech.gruul.web.api.util.GruulUtil;
import cn.zswltech.gruul.common.util.StringUtil;
import cn.zswltech.gruul.common.entity.CustomTreeDO;
import cn.zswltech.gruul.web.api.controller.common.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
public class CustomTreeController extends BaseController implements CustomerTreeApi {

    @Resource(name = "customTreeService")
    private CustomTreeService customTreeService;

    /**
     * 分组排序
     * @param req
     * @return
     * @throws Exception
     */
    @Override
    public Response groupSort(GroupSortREQ req) {
        return customTreeService.groupSort(req.getGroupUuids());
    }

    /**
     * 分组下的菜单排序
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response menuSort(MenuSortREQ req) {
        return customTreeService.menuSort(req.getGuoupId(), req.getMenuUuids());
    }

    /**
     * 获取全部分组下的菜单
     *
     * @return
     * @throws Exception
     */
    @Override
    @GetMapping
    public Response<List<CustomTreeDO>> getCustomTreeList() {
        return customTreeService.getCustomTreeList();
    }


//    /**
//     * 创建分组
//     *
//     * @return
//     * @throws Exception
//     */
//    @Override
//    public Response menuGroupAdd() {
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        CustomTreeDO customTreeDO = genCustomTreeDO(request);
//        Date now = new Date();
//        customTreeDO.setGmtCreate(now);
//        customTreeDO.setGmtModified(now);
//        customTreeDO.setParentId(Long.valueOf(request.getParameter("parentId")));
//        customTreeDO.setSortNo(0);
//        return customTreeService.createCustomTree(customTreeDO);
//    }

    /**
     * 更新分组
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response updateCustomTreeGroup() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        CustomTreeDO customTreeDO = genCustomTreeDO(request);
        customTreeDO.setId(Long.valueOf(request.getParameter("id")));
        return customTreeService.updateCustomTree(customTreeDO);
    }

    /**
     * 删除分组
     *
     * @return
     * @throws Exception
     */
    @Override
    public Response deleteCustomTreeGroup(Long id) {
        return customTreeService.deleteCustomTree(id);
    }

    /**
     * 自定义分组关联菜单
     *
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public Response createCustomTreeMenu(String params) {
        return customTreeService.createCustomTreeMenu(params);
    }

    @Override
    public Response<List<MenuTreeInfo>> appMenuTree() {
        List<MenuTreeInfo> result;
        try {
          result =  customTreeService.appMenuTree(GruulUtil.getSystemCode());
        } catch (Exception e) {
            log.error("查询菜单树分组报错", e);
            return Response.error(500, "菜单树查询出错");
        }
        return Response.success(result);
    }

    @Override
    public Response addMenuGroup(MenuGroupAddREQ req) {
        try {
            customTreeService.addMenuGroup(req);
        } catch (MenuException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(MenuErrorEnum.MENU_GROUP_ADD_ERROR.getErrMsg(), e);
        }
        return Response.success();
    }

    @Override
    public Response updateMenuGroup(MenuGroupUpdateREQ req) {
        try {
            customTreeService.updateMenuGroup(req);
        } catch (Exception e) {
            log.error("菜单分组修改失败 req:{}", req,e);
            return Response.error(500, "菜单分组修改失败");
        }
        return Response.success();
    }

    /**
     * 删除菜单分组 相关菜单自动解绑
     * @param req 分组id
     * @return
     */
    @Override
    public Response delMenuGroup(MenuGroupDelREQ req) {
        try {
            customTreeService.delMenuGroup(req);
        } catch (BaseException e) {
            log.error(e.getErrMsg(), e);
            return Response.error(e);
        }catch (Exception e){
            log.error(MenuErrorEnum.MENU_GROUP_DEL_ERROR.getErrMsg(), e);
            return Response.error(MenuErrorEnum.MENU_GROUP_DEL_ERROR);
        }
        return Response.success();
    }

    private CustomTreeDO genCustomTreeDO(HttpServletRequest request) {
        CustomTreeDO customTreeDO = new CustomTreeDO();
        customTreeDO.setCode(request.getParameter("code"));
        customTreeDO.setName(request.getParameter("name"));
        customTreeDO.setEnName(request.getParameter("enName"));
        customTreeDO.setIcon(request.getParameter("icon"));
        customTreeDO.setPath(request.getParameter("path"));
        customTreeDO.setFlag(StringUtil.str2Integer(request.getParameter("flag")));

        return customTreeDO;
    }
}
